package SoftWareHomework;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.junit.jupiter.api.Assertions.*;

class StringUtilsTest {

    private StringUtils s;
    @BeforeEach
    void setUp() {
       s=new StringUtils();
    }


    @Test
    void reverseNormal() {
        String result = s.reverse("hello");
        assertEquals("olleh", result);
    }
    @Test
    void reverseEmpty() {
        String result =s.reverse("");
        assertEquals("", result);
    }

    @Test
    void reverseSingleCharacter() {
        String result =s.reverse("a");
        assertEquals("a", result);
    }


    @Test
    void testIsPalindromeNormalCase() {
        assertTrue(s.isPalindrome("madam"));
        assertFalse(s.isPalindrome("hello"));
    }

    @Test
    void testIsPalindromeSingleCharacter() {
        assertTrue(s.isPalindrome("a"));
    }

    @Test
    void testSubstringNormalCase() {
        String result = s.substring("hello", 1, 4);
        assertEquals("ell", result);
    }

    @Test
    void testSubstringEmptyString() {
        String result =s.substring("", 0, 1);
        assertEquals("", result);
    }

    @Test
    void testSubstringNegativeStartIndex() {
        String result = s.substring("hello", -1, 4);
        assertEquals("", result);  // 如果索引无效，返回空字符串
    }

    @Test
    void testSubstringStartGreaterThanEnd() {
        String result = s.substring("hello", 4, 1);
        assertEquals("", result);  // start > end 时返回空字符串
    }

    @Test
    void testSubstringNullString() {
        String result = s.substring(null, 0, 1);
        assertEquals("", result);  // 如果字符串是 null，返回空字符串
    }


    //参数化测试
    @ParameterizedTest
    @CsvSource({
            "'hello', 'olleh'",
            "'a', 'a'",
            "'madam', 'madam'",
            "'', ''",
            "'12345', '54321'"
    })
    void testReverseParameterized(String input, String expected) {
        String result = s.reverse(input);
        assertEquals(expected, result);
    }

    @ParameterizedTest
    @CsvSource({
            "'madam', true",
            "'hello', false",
            "'', true",
            "'a', true'",
            "'12321', true'"
    })
    void testIsPalindromeParameterized(String input, boolean expected) {
        boolean result = s.isPalindrome(input);
        assertEquals(expected, result);
    }

    @ParameterizedTest
    @CsvSource({
            "'hello', 1, 4, 'ell'",
            "'hello', 0, 5, 'hello'",
            "'hello', 1, 10, 'ello'",
            "'hello', 4, 1, ''",
            "'hello',-1,3"
    })
    void testSubstringParameterized(String input, int start, int end, String expected) {
        String result = s.substring(input, start, end);
        assertEquals(expected, result);
    }

}